package com.lx.javasunmmrize.JUC;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Description TODO
 * @Author lx
 * @Date 2021/3/2 22:32
 **/
public class ThreadTest01 {

    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        //匿名内部类简化成lamda表达式
        new Thread(() -> {
            for (int i = 0; i < 30; i++) {
                ticket.sele();
            }
        }, "A").start();
        new Thread(() -> {
            for (int i = 0; i < 30; i++) {
                ticket.sele();
            }
        }, "B").start();
        new Thread(() -> {
            for (int i = 0; i < 30; i++) {
                ticket.sele();
            }
        }, "C").start();

        //查看CPU核数,CPU密集型,IO密集型
//        System.out.println(Runtime.getRuntime().availableProcessors());
    }
}

//资源类
class Ticket {
    Lock lock = new ReentrantLock();


    private int num = 20;//原始的票数量

    //卖票方法
    public  synchronized void sele() {






        if (num > 0) {
            System.out.println(Thread.currentThread().getName() + "迈出了" + num-- + ",剩余" + num);
        }
    }

}